﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.SessionState;
using Common;
using IBll;
using Model;
using Model.EasyUiModel;

namespace MvcOa.Helper
{
    public class OperationContext
    {
        #region HttpContext属性

        HttpResponse Response
        {
            get { return HttpContext.Current.Response; }
            set { value = HttpContext.Current.Response; }
        }

        HttpRequest Request
        {
            get { return HttpContext.Current.Request; }
            set { value = HttpContext.Current.Request; }
        }

        HttpSessionState Session
        {
            get { return HttpContext.Current.Session; }
            set { value = HttpContext.Current.Session; }
        } 
        #endregion

        #region 静态方式访问对象

        /// <summary>
        /// 线程中获取上下文
        /// </summary>
        public static OperationContext Current
        {
            get
            {
                OperationContext operationContext =
                    CallContext.GetData(typeof (OperationContext).Name) as OperationContext;
                if (operationContext==null)
                {
                    operationContext=new OperationContext();
                    CallContext.SetData(typeof(OperationContext).Name,operationContext);
                }
                return operationContext;
            }
        }

        #endregion

        #region 全局属性
        public IBllSession IBllSession
        {
            get
            {
                return DI.SpringHelper.GetObject<IBllSession>("BllSession");
            }
        }

        public List<Permission> ListPermissions
        {
            get
            {
                if (SessionHelper.GetValue<List<Permission>>("PermissionList")==null)
                {
                    if (CurrentUser!=null)
                    {
                        List<Permission> listpPermissions =
                            IBllSession.IUserInfoBll.GetUserPermission(CurrentUser.uId).ToList().Select(m => m.ToPoco()).ToList();
                        SessionHelper.SetValue("PermissionList", listpPermissions);
                    }
                }
                return SessionHelper.GetValue<List<Permission>>("PermissionList");
            }
            set
            {
                SessionHelper.SetValue("PermissionList", value);
            }
        }

        public List<PermissionTreeNodeModel> ListPermissionTreeNodeModels
        {
            get
            {
                if (SessionHelper.GetValue<List<PermissionTreeNodeModel>>("ListTreeNodes") == null)
                {
                    List<PermissionTreeNodeModel> listTreeNodes = Permission.ToTreeNodes(ListPermissions.Where(m => m.pIsShow == true).ToList());
                    SessionHelper.SetValue("ListTreeNodes", listTreeNodes);
                }
                return SessionHelper.GetValue<List<PermissionTreeNodeModel>>("ListTreeNodes");
            }
            set
            {
                SessionHelper.SetValue("ListTreeNodes", value);
            }
        }
        public UserInfo CurrentUser
        {
            get
            {
                if (SessionHelper.GetValue<UserInfo>("CurrentUser")==null)
                {
                    //no session find cookie
                    if (!string.IsNullOrEmpty(CookieHelper.GetValue("uid")))
                    {
                        int uid = CookieHelper.GetValue("uid").ToInt();
                        UserInfo userInfo = IBllSession.IUserInfoBll.GetAll().First(m => m.uId == uid);
                        if (userInfo != null)
                        {
                            SessionHelper.SetValue("CurrentUser", userInfo);
                        }
                    }
                }
                return SessionHelper.GetValue<UserInfo>("CurrentUser");
            }
            set
            {
                SessionHelper.SetValue("CurrentUser", value);
            }
        } 
        #endregion

        public bool IsLogin()
        {
            if (CurrentUser != null || CookieHelper.GetValue("uid") != null)
            {
                return true;
            }
            return false;
        }

        
    }
}
